package com.access.boot.starter.sample.web;

import com.access.boot.apisdk.SdkContext;
import com.access.boot.apisdk.SdkManager;
import com.access.boot.apisdk.mock.RegionMock;
import com.access.boot.apisdk.mock.SecretMock;
import com.access.boot.apisdk.mock.TokenMock;
import com.access.boot.starter.sample.openapi.TestOpenapiServiceV1;
import com.access.boot.starter.sample.openapi.TestOpenapiServiceV2;
import com.access.boot.starter.sample.openapi.model.UserVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * 对外暴露的接口，底层调用Openapi
 *
 * @author Yangzhen
 * @date 2023/1/16 13:49
 * @since 1.0.0
 */
@RestController
@RequestMapping("/call/openapi")
public class CallOpenapiController {

    // 接口实例化
    private TestOpenapiServiceV1 testOpenapiServiceV1 = SdkManager.get(TestOpenapiServiceV1.class);

    /**
     * 手动创建Openapi上下文
     *
     * @param groupId
     * @return
     */
    @GetMapping("v1/{group_id}/users")
    public Object getListWithSdkContext(@PathVariable("group_id") String groupId) {
        SdkContext sdkContext = getSdkContext();
        List<UserVO> list = testOpenapiServiceV1.getList(groupId, sdkContext);
        return list;
    }

    /**
     * 手动创建Openapi调用的上下文
     * 真实的构建方式远比这种复杂的多，这里模拟构建
     *
     * @return
     */
    public SdkContext getSdkContext() {
        String clientId = UUID.randomUUID().toString();
        // 获取密钥信息
        String secret = SecretMock.getSecret(clientId);
        // 获取部署区域
        String region = RegionMock.getRegion();
        // 获取token
        String token = TokenMock.getToken();
        // 获取SdkContext
        SdkContext sdkContext = SdkContext.newInstance(region, clientId, secret, token);
        return sdkContext;
    }

    /**
     * 手动创建Openapi上下文
     *
     * @param groupId
     * @return
     */
    @GetMapping("v1/{group_id}/user")
    public Object getUserWithSdkContext(@PathVariable("group_id") String groupId, @RequestParam("user_id") String userId) {
        SdkContext sdkContext = getSdkContext();
        UserVO user = testOpenapiServiceV1.getUser(groupId, userId, sdkContext);
        return user;
    }

    // Spring自动注入
    @Resource
    private TestOpenapiServiceV2 testOpenapiServiceV2;

    /**
     * 自动创建Openapi上下文
     *
     * @param groupId
     * @return
     */
    @GetMapping("v2/{group_id}/users")
    public Object getList(@PathVariable("group_id") String groupId) {
        List<UserVO> list = testOpenapiServiceV2.getList(groupId);
        return list;
    }

    /**
     * 自动创建Openapi上下文
     *
     * @param groupId
     * @return
     */
    @GetMapping("v2/{group_id}/user")
    public Object getUser(@PathVariable("group_id") String groupId, @RequestParam("user_id") String userId) {
        UserVO user = testOpenapiServiceV2.getUser(groupId, userId);
        return user;
    }
}
